#! /bin/sh 
# This is the main backup script.  It reads the backup schedule and 
# calls backup script and export script to perform the necessary 
# action. 
# 
# name		$TOOLS/backup/dbbackup 
# 
# Note: $TOOLS variable is set to the directory where all system  
# administration scripts. 
#   
# purpose	Perform a backup of a database. 
# 
# usage		$TOOLS/backup/dbbackup <dbname> 
# parameters	$1=dbname 
# 
#               Calls $TOOLS/db_admin/backup/common/crontab.env 
#                     to set required enironment variables 
#		Calls $TOOLS/backup/dbbackup_begin or 
#                     $TOOLS/backup/dbexport_begin 
#                     depending on the schedule 
# 
# .............................................................. 
# set local variables: Files to log messages, errors, etc.  
# .............................................................. 
BEGIN_JOB="`date`" 
ERRMSG="Usage: `basename $0` <dbname>" 
 
if [ "$1" ] 
then DBNAME=$1 
else echo $ERRMSG 
     exit 
fi 
 
LOGDIR="$TOOLS/db_admin/db_${DBNAME}/log" 
 
LOGFILE="${LOGDIR}/backup_`date '+%y%m%d'`.log" 
ERRFILE="${LOGDIR}/backup_`date '+%y%m%d'`.err" 
ERRFILE2="${LOGDIR}/backup_`date '+%y%m%d'`_old.err" 
MSGFILE="${LOGDIR}/backup_`date '+%y%m%d'`.msg" 
 
# 
# .............................................................. 
# set environment 
# .............................................................. 
 
# This should define ORACLE_HOME, ORACLE_SID, PATH, BACKUPDIR 
# BACKUPDIROLD, EXPORTDIR, EXPORTDIROLD, ARCOLD 
#  
. $TOOLS/db_admin/common/crontab.env $DBNAME 
 
 
# ADMIN_FILE is a file that can be used to run some administrative commands  
# on a given database. One such task could be changing a parameter in 
# init<sid>.ora. Uncomment the line if required. 
# 
# ADMIN_FILE="$TOOLS/db_admin/db_$DBNAME/tools/${DBNAME}_backup_admin.sh" 
 
# 
# SCHED_FILE is the file containing backup schedule.  A typical line in 
# this file is: 
# V7PROD Sun hot export /V7PROD/db_management/restrict1.sh 
# where V7PROD is name of the database, Sun is for Sunday, hot is the 
# backup type, and export is the additinal backup procedure.  The last  
# column is a special tak you want to perform, for example cleanup 
# of some files etc.   
# 
# JOBNAME is the current script. 
# DBBACKUP_BEGIN is the backup script that performs hot or cold backup 
# based on the schedule of the day.   
# DBEXPORT_BEGIN is the script for exporting the database. 
# dbbackup_begin and dbexport_begin are shown in this section. 
#  
 
 
SCHED_FILE="$TOOLS/db_admin/common/dbbackup_sched.dat" 
JOBNAME="$TOOLS/backup/dbbackup" 
DBBACKUP_BEGIN="$TOOLS/backup/dbbackup_begin" 
DBEXPORT_BEGIN="$TOOLS/backup/dbexport_begin" 
TODAY="`date`" 
THIS_DAY="`date '+%a'`" 
MSG="$DBNAME Backup succeeded at `date`" 
 
# This has to be nawk for most paltforms, except HP-UX 
AWK=/usr/bin/nawk 
# bdf works on HP-UX, df -k on Solaris 2.x, df on SunOS 4.x 
# You might also want to add other options, like 'df -kF ufs' for Solaris. 
DF="/usr/bin/df -k" 
 
# .............................................................. 
# begin 
# .............................................................. 
if [ -f "$ERRFILE" ]; then 
  #  
  # Save old log file 
  # 
  cat $ERRFILE >> $ERRFILE2 
fi 
# 
# Read backup schedule 
# 
$AWK -v dbname=$DBNAME -v this_day=$THIS_DAY '{ 
    # 
    # get a record 
    # 
    cmd=$0 
    sizeofarray=split(cmd,rec," ") 
    dbname2=rec[1] 
    day_of_week=rec[2] 
    backup=rec[3] 
    export=rec[4] 
    special_task=rec[5] 
 
    if (( dbname2 == dbname ) && ( this_day == day_of_week ))  
	print " " backup " " export " " special_task   
}' $SCHED_FILE | while read BACKUP EXPORT SPECIAL_TASK 
do 
 
  # 
  # Print all parameters before beginning backup.  
  # 
  PARAMETER_MSG=" 
\n...................................................................... 
\nBackup Job Parameters: 
\n 
\nDatabase Name = $DBNAME 
\nBackup Type   = $BACKUP 
\nExport Type   = $EXPORT 
\nSpecial Task  = $SPECIAL_TASK 
\n 
\nEnvironment Variables: 
\nORACLE_HOME   = $ORACLE_HOME 
\nORACLE_SID    = $ORACLE_SID 
\nPATH          = $PATH 
\n...................................................................... 
\n 
" 
  echo $PARAMETER_MSG >> $LOGFILE 2> $ERRFILE 
  echo $PARAMETER_MSG >> $MSGFILE 
  $DF >> $LOGFILE 2> $ERRFILE 
  echo " " >> $LOGFILE 2> $ERRFILE 
 
  # 
  # Backup 
  #  
  if [ "$BACKUP" != "nobackup" ]; then 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
    echo "Begin backup at `date`" >> $LOGFILE 2>> $ERRFILE 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
    $DBBACKUP_BEGIN $DBNAME $BACKUP $SPECIAL_TASK >> $LOGFILE 2>> $ERRFILE 
    echo "$DBBACKUP_BEGIN $DBNAME $BACKUP $SPECIAL_TASK >> $LOGFILE 2>> 
$ERRFILE" 
    echo 
    echo 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
    echo "End backup at `date`" >> $LOGFILE 2>> $ERRFILE 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
  fi 
 
  # 
  # Export 
  # 
  if [ "$EXPORT" != "noexport" ]; then 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
    echo "Begin export at `date`" >> $LOGFILE 2>> $ERRFILE 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
    $DBEXPORT_BEGIN $DBNAME $EXPORT $SPECIAL_TASK >> $LOGFILE 2>> $ERRFILE 
    echo "$DBEXPORT_BEGIN $DBNAME $EXPORT $SPECIAL_TASK >> $LOGFILE 2>> 
$ERRFILE"  
    echo 
    echo 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
    echo "End export at `date`" >> $LOGFILE 2>> $ERRFILE 
    echo "..................................." >> $LOGFILE 2>> $ERRFILE 
  fi 
 
  echo " " >> $MSGFILE 
  echo "Backup log file errors and warnings:" >> $MSGFILE 
  
  echo " " >> $LOGFILE 2>> $ERRFILE 
  $DF >> $LOGFILE 2>> $ERRFILE 
  echo " " >> $LOGFILE 2>> $ERRFILE 
 
  # 
  # Errors 
  # 
  egrep -e error -e warning -e ORA- -e EXP- -e fatal $LOGFILE | grep -v "No 
errors." >> $MSGFILE  
  ERRCNT=`egrep -e error -e ORA- -e EXP- -e fatal $LOGFILE | grep -c -v "No 
errors."`  
  egrep -e error -e warning -e ORA- -e EXP- -e fatal $ERRFILE | grep -v 
"Export terminated successfully" >> $MSGFILE 
  ERRCNT2=`egrep -e error -e ORA- -e EXP- -e fatal $ERRFILE | grep -c -v 
"Export terminated successfully"` 
  END_JOB="`date`" 
  if [ "$ERRCNT" -gt 0 -o "$ERRCNT2" -gt 0 ] 
  then MSG="$DBNAME backup failed at ${END_JOB}" 
  else MSG="$DBNAME backup succeeded at ${END_JOB}" 
  fi 
 
  echo " " >> $MSGFILE 
  echo "Log files: " >> $MSGFILE 
  echo "Log file=$LOGFILE" >> $MSGFILE 
  echo "Error file=$ERRFILE" >> $MSGFILE 
  echo "Message file=$MSGFILE" >> $MSGFILE  
 
  # Create database Space Report 
  # The space_report script should contain SQL statements needed to get 
  # information about the available space in the database. One example: 
  # 
  #   svrmgrl <<EOF 
  #   connect internal 
  #   spool <file_name> 
  #   select tablespace_name, sum(bytes) from dba_free_space 
  #    group by tablespace_name; 
  #   exit 
  # EOF 
  # 
  # Uncomment the next line if you have set up the script. 
 
  # $TOOLS/sql/space_report $DBNAME 
 
  # 
  # Send mail to all DBAs  
  # Uncomment the next line if you want a mail to be sent to all DBA's 
  # at the end of a backup job. We assume that a mailing list or alias  
  # "${DBNAME}_dbas" exists in your system. If not, modify the sender name 
  # appropriately. The mail command also may have to be changed depending 
  # on the platform. 
  #     /usr/bin/mailx -s "$MSG" ${DBNAME}_dbas < $MSGFILE 
 
  # 
# Uncomment the next line if you have set the ADMIN_FILE variable. 
#  . $ADMIN_FILE 
done 
 
 
